<script setup>
import { getWhitelistDetail, obtainWhitelist } from '@/api/my';
import { jump } from '@/utils/tool';

//添加响应式变量
const time = ref('');
const whitelistId = ref(0);
const businessid = ref(0);
const uuid = ref(0);

//扫码后获取白名单用户权益
const whitelistDetail = async () => {
  try {
    const res = await getWhitelistDetail({ whitelistId: whitelistId.value, uuid: uuid.value });
    time.value = res.data / 60;
  } catch (error) {
    console.error(' 获取白名单详细信息出错:', error);
    // 跳转到指定的白名单页面
    uni.reLaunch({
      url: '/pages/error/error'
    });
  } finally {
    // 隐藏加载动画
    uni.hideLoading();
  }
};

//点击添加白名单
const receive = async () => {
  try {
    const res = await obtainWhitelist({ whitelistId: whitelistId.value, businessId: businessid.value, uuid: uuid.value });
    if (res.code === 500) {
      uni.showModal({
        title: '提示',
        content: '不能重复添加',
        success(a) {
          console.log('a', a);
          jump('/pages/home/home');
        }
      });
    } else {
      uni.showModal({
        title: '提示',
        content: '操作成功',
        success(a) {
          jump('/pages/home/home');
        }
      });
    }
  } catch (error) {
    uni.showModal({
      title: '提示',
      content: error.msg || '操作失败，请重试',
      success(a) {
        jump('/pages/home/home');
      }
    });
  }
};

/**
 * 生命周期函数--监听页面加载
 */
onLoad(() => {
  const options = uni.getLaunchOptionsSync();

  if (options.query.q) {
    const scene = decodeURIComponent(options.query.q);
    const [ws, bs, us] = scene.split('&');
    const wid = ws.split('=')[1];
    const bid = bs.split('=')[1];
    const value = us ? us.split('=')[1] : 0;

    uni.setStorageSync('kwid', wid);
    uni.setStorageSync('kbid', bid);
    uni.setStorageSync('whiteuuid', value);

    whitelistId.value = wid;
    businessid.value = bid;
    uuid.value = value;
  } else {
    whitelistId.value = uni.getStorageSync('kwid');
    businessid.value = uni.getStorageSync('kbid');
    uuid.value = uni.getStorageSync('whiteuuid');
  }

  whitelistDetail();
});
</script>

<template>
  <view class="receive" style="background: url('/static/images/icon_whitelist.png') no-repeat center center/cover">
    <view class="top">
      <view style="padding-top: 50%; color: #000">白名单用户权益</view>
      <view style="margin-top: 5%; color: #000">在该商家所属网点租借共享充电宝可免费使用{{ time }}小时，超过{{ time }}小时后开始正常计费</view>
    </view>
  </view>
  <view class="buttom" @tap="receive">
    <view class="text_3" style="color: #fff">立即添加</view>
  </view>
</template>

<style lang="scss">
page {
  background-color: #f67b0e;
}
.receive {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 61rpx 0 62rpx 61rpx;
  width: 627rpx;
  height: 857rpx;
  .top {
    height: 592rpx;
    width: 444rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}
.buttom {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 75rpx;
  width: 600rpx;
  height: 90rpx;
  background-color: #000;
  box-shadow: 0rpx 16rpx 27rpx 0rpx rgba(255, 68, 7, 0.3);
  border-radius: 60rpx;
  .text_3 {
    font-size: 40rpx;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
  }
}
</style>
